package life.graph;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Properties;

import life.Coordinate;
import life.Food;
import life.Liso;

public class LisoReader
{
	private String path;
	private ArrayList<Food> foodMap = new ArrayList<Food>();
	private ArrayList<Liso> lisoMap = new ArrayList<Liso>();
	private int cycle;
	private String pathStatFile;

	public LisoReader(String path)
	{
		this.path = path;
	}

	public void process()
	{
		try
		{

			FileInputStream fstream = new FileInputStream(path);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null)
			{
				String parts[] = strLine.split(";");
				if (parts[0].equalsIgnoreCase("l"))
				{
					Liso l = new Liso();
					l.setEnergy(Double.parseDouble(parts[1]));
					Coordinate pos = new Coordinate(Double.parseDouble(parts[2]), Double.parseDouble(parts[3]));
					l.setPosition(pos);
					Coordinate v = new Coordinate(Double.parseDouble(parts[4]), Double.parseDouble(parts[5]));
					l.setVel(v);
					l.setAge(Double.parseDouble(parts[6]));
					l.setGeneration(Integer.parseInt(parts[7]));

					Properties prop = new Properties();
					String propParts[] = parts[8].split("#");
					for (String p : propParts)
					{
						String pp[] = p.split("=");
						prop.setProperty(pp[0], pp[1]);
					}
					l.setProp(prop);
					lisoMap.add(l);
				}
				else if (parts[0].equalsIgnoreCase("f"))
				{
					Food f = new Food();
					f.setEnergy(Double.parseDouble(parts[1]));
					Coordinate pos = new Coordinate(Double.parseDouble(parts[2]), Double.parseDouble(parts[3]));
					f.setPosition(pos);
					f.setAge(Double.parseDouble(parts[4]));
					f.setFoodLifeSpan(Double.parseDouble(parts[5]));
					foodMap.add(f);
				}
				else if (parts[0].equalsIgnoreCase("cycle"))
				{
					cycle=Integer.parseInt(parts[1]);
				}
				else if (parts[0].equalsIgnoreCase("pathStatFile"))
				{
					pathStatFile=parts[1];
				}
			}
			in.close();
		}
		catch (Exception e)
		{
			System.err.println("Error: reading from:"+path+" " + e.getMessage());
		}
	}

	public ArrayList<Liso> getLisoMap()
	{
		return lisoMap;
	}

	public ArrayList<Food> getFoodMap()
	{
		return foodMap;
	}

	public int getCycle()
	{
		return cycle;
	}

	public String getPathStatFile()
	{
		return pathStatFile;
	}

}
